﻿using System;
using System.Collections.Generic;

namespace CodeContrib
{
	public class PredicateEqualityComparer<T> : EqualityComparer<T>
	{
		private readonly Func<T, T, bool> _predicate;

		public PredicateEqualityComparer(Func<T, T, bool> predicate)
		{
			_predicate = predicate;
		}

		public override bool Equals(T x, T y)
		{
			if (x != null)
			{
				return ((y != null) && _predicate(x, y));
			}

			if (y != null)
			{
				return false;
			}

			return true;
		}

		public override int GetHashCode(T obj)
		{
			// Always return the same value to force the call to IEqualityComparer<T>.Equals.
			return 0;
		}
	}
}